//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System.Collections;
using CommunityServer.Components;

namespace CommunityServer.Galleries.Components
{
	/// <summary>
	/// Provides access to Groups of Galleries
	/// </summary>
	public class GalleryGroups
	{
        /// <summary>
        /// Private. Can not be instantiated
        /// </summary>
		private GalleryGroups()
		{}

        /// <summary>
        /// Returns the specificied Group.
        /// </summary>
        public static Group GetGroup(int groupID, bool cacheable, bool ignorePermissions, bool flush)
        {
            ArrayList groups = GetGroups(cacheable,ignorePermissions,flush);
            foreach(Group fg in groups)
            {
                if(fg.GroupID == groupID)
                    return fg;
            }
            return null;
        }

        /// <summary>
        /// Returns a list of Gallery Groups the specific user has access to (access is 
        /// defined as one of the blog is visible to the user)
        /// </summary>
        public static ArrayList GetGroups(bool cacheable, bool ignorePermissions, bool flush)
        {
            CSContext csContext = CSContext.Current;
            ArrayList groups = new ArrayList();
            Hashtable groupsTable;

            // Get the GalleryGroups as a hashtable
            //
            groupsTable = GetGroups(csContext, cacheable, ignorePermissions,flush);

            // Hide gallery groups based on the user's cookie
            //
            groupsTable = Groups.HideGroups(groupsTable);

            // Convert the hashtable to an arraylist
            //
            foreach(Group fg in groupsTable.Values)
                groups.Add( fg );

            // Sort the gallery groups
            //
            groups.Sort();

            return groups;
        }

        /// <summary>
        /// Internal method for looking up all of the gallery group data.
        /// </summary>
        private static Hashtable GetGroups(CSContext csContext, bool cacheable, bool ignorePermissions, bool flush) 
        {
            int settingsID = CSContext.Current.SiteSettings.SettingsID;
            string cacheKey = string.Format("AllGalleryGroups{0}-Cacheable:{1}-IgnorePermissions:{2}",settingsID,cacheable,ignorePermissions);

			if(flush)
			{
				csContext[cacheKey] = null;
				CSCache.Remove(cacheKey);
			}

            Hashtable unfilteredGroups;

            // Evict item from cache if not cacheable
            //
            if (!cacheable)
                CSCache.Remove(cacheKey);

			unfilteredGroups = csContext[cacheKey] as Hashtable;
			if(unfilteredGroups != null)
				return unfilteredGroups;

            // Get the raw gallery groups
            //
            unfilteredGroups = CSCache.Get(cacheKey) as Hashtable;
            if ( unfilteredGroups == null ) 
            {
                unfilteredGroups =  CommonDataProvider.Instance().GetGroups(ApplicationType.Gallery, false);

                if (cacheable)
                    CSCache.Insert(cacheKey,unfilteredGroups,CSCache.HourFactor);

            } 

            // Are we ignoring permissions?
            //
			if (ignorePermissions)
			{
				csContext[cacheKey] = unfilteredGroups;
				return unfilteredGroups;
			}

            // We need to create a new hashtable
            //
            Hashtable groups = new Hashtable();

            // Filter the list of forum groups to only show gallery groups
            // this user is allowed to see, i.e. if all the galleries in this
            // group are private and the user is not allowed to see them
            //
            foreach (Group group in unfilteredGroups.Values) 
            {

                // Get the associated galleries
                //
                if (!cacheable) 
                    groups.Add(group.GroupID, group);
                else if (Galleries.GetGalleriesByGroupID(group.GroupID,ignorePermissions, cacheable,false).Count > 0)
                    groups.Add(group.GroupID, group);

            }

			csContext[cacheKey] = groups;

            return groups;

        }

		/// <summary>
		/// Returns true if the Group has Sections
		/// </summary>
		public static bool HasChildSections(int groupID)
		{
			return CommunityServer.Galleries.Galleries.GetGalleriesByGroupID(groupID,true,true,true).Count > 0;
		}

	}
}
